import agentHttp from '@/utils/agentHttp.js'

export default {
  name: 'password',
  created(){
   
  },

  data() {
    var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.dialogForm.confirmPassword !== '') {
            this.$refs.dialogForm.validateField('confirmPassword');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.dialogForm.newPassword) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
  	return {
      dialogForm:{
        password:"",
        newPassword:"",
        confirmPassword:""
      },
      formLabelWidth:'120px',
      rules:{
         password: [{required:true}],
         newPassword:[{required:true,message:"新密码不能为空"},{ validator: validatePass, trigger: 'blur' }],
         confirmPassword:[{required:true,message:"确认新密码不能为空"},{ validator: validatePass2, trigger: 'blur' }]
      }
  	}
  },
  methods: {
    returnBack(){
      window.history.go(-1);
    },
    //提交按钮
    submitDialogForm(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {                 
          this.editData();//修改              
        } else {
          this.$nextTick(()=>{
          this.$message.warning(document.getElementsByClassName('el-form-item__error')[0].innerText);
          return false;
          })
        }
      });
    },
    //修改提交
    editData(){
      var data = {};
      data.password = this.dialogForm.password;
      data.newPassword = this.dialogForm.newPassword;
      agentHttp.post('adminApi/admin/user/update_password',this.dialogForm).then((data)=>{
          if(data.success){  
            this.$message.success(data.data||"修改成功");
            setTimeout(()=>{
              this.$store.dispatch('LogOut').then(() => {
                location.reload()// 为了重新实例化vue-router对象 避免bug
              }) 
            },500)                            
          } 
      }) 
    }
  }
}